﻿using LeSoft.Application.Bussiness;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace LeSoft.Application.Web.Areas.SystemManage.Controllers
{
    public class AreaController : MvcControllerBase
    {
        private AreaBLL areaBLL = new AreaBLL();

        #region 视图功能
        /// <summary>
        /// 区域管理
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 区域表单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult Form()
        {
            return View();
        }
        /// <summary>
        /// 区域详细
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult Detail()
        {
            return View();
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 区域列表 
        /// </summary>
        /// <param name="value">当前主键</param>
        /// <returns>返回树形Json</returns>
        [HttpGet]
        public ActionResult GetTreeJson(string value, string keyword)
        {
            string parentId = value == null ? "0" : value;
            var filterdata = areaBLL.GetList(parentId, keyword).ToList();
            var treeList = new List<TreeEntity>();
            if (filterdata != null)
            {
                foreach (var item in filterdata)
                {
                    #region 房产
                    TreeEntity tree = new TreeEntity();
                    tree.id = item.AreaId;
                    tree.text = item.AreaName;
                    tree.value = item.AreaId;
                    tree.title = item.AreaCode;
                    tree.Attribute = "LAT";
                    tree.AttributeValue = item.LAT == null ? "" : item.LAT.ToString();
                    tree.parentId = "0";
                    tree.AttributeA = "LON";
                    tree.AttributeValueA = item.LON == null ? "" : item.LON.ToString();
                    tree.isexpand = false;
                    tree.complete = true;
                    tree.hasChildren = false;
                    treeList.Add(tree);
                    #endregion
                }
            }
            return Content(treeList.TreeToJson());
        }
        /// <summary>
        /// 区域列表
        /// </summary>
        /// <param name="value">当前主键</param>
        /// <param name="keyword">关键字查询</param>
        /// <returns>返回列表Json</returns>
        [HttpGet]
        public ActionResult GetListJson(string value, string keyword)
        {
            string parentId = value == null ? "0" : value;
            var data = areaBLL.GetList(parentId, keyword).ToList();
            return Content(data.ToJson());
        }
        /// <summary>
        /// 区域列表（主要是绑定下拉框）
        /// </summary>
        /// <param name="parentId">节点Id</param>
        /// <returns>返回列表Json</returns>
        [HttpGet]
        public ActionResult GetAreaListJson(string parentId)
        {
            var data = areaBLL.GetAreaList(parentId == null ? "0" : parentId);
            return Content(data.ToJson());
        }
        /// <summary>
        /// 区域列表返回所有列表
        /// </summary>
        /// <param name="parentId">节点Id</param>
        /// <returns>返回列表Json</returns>
        public ActionResult GetList2Json()
        {
            var data = areaBLL.GetList();
            return Content(data.ToJson());
        }
        /// <summary>
        /// 区域实体 
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <returns>返回对象Json</returns>
        [HttpGet]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = areaBLL.GetEntity(keyValue);
            return Content(data.ToJson());
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 删除区域
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult RemoveForm(string keyValue)
        {
            areaBLL.RemoveForm(keyValue);
            return Success("删除成功。");
        }
        /// <summary>
        /// 保存区域表单（新增、修改）
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <param name="areaEntity">区域实体</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult SaveForm(string keyValue, AreaEntity areaEntity)
        {
            areaBLL.SaveForm(keyValue, areaEntity);
            return Success("操作成功。");
        }
        #endregion
    }
}
